package cn.mw.cmdb.entity.modelRelation;

import cn.mw.microMonitorCommon.util.ListMapObjUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Data
@Slf4j
public class ModelRelationGroupInfo {
    private String id;
    private String label;
    private String realGroupId;
    private List<ModelRelationOppoInfo> modelList = new ArrayList<>();

    public void extractFrom(RelationGroupDTO data, List<ModelRelationDTO> relationDTOS, String ownModelId) {
        this.id = "group" + data.getId();
        this.label = data.getRelationGroupName();
        this.realGroupId = data.getId();

        if (null != relationDTOS) {
            for (ModelRelationDTO modelRelationDTO : relationDTOS) {
                ModelRelationOppoInfo modelRelationOppoInfo = new ModelRelationOppoInfo();
                Map map = modelRelationDTO.getRelationInfoMap();
                for (Object mapData : map.values()) {
                    try {
                        ModelRelationInfo modelRelationInfo = ListMapObjUtils.mapToBean((Map) mapData, ModelRelationInfo.class);
                        if (!StringUtils.equals(modelRelationInfo.getModelId(), ownModelId)) {
                            modelRelationOppoInfo.setValue(modelRelationInfo.getModelId());
                            modelRelationOppoInfo.setNum(Integer.parseInt(modelRelationInfo.getNum()));
                        } else {
                            modelRelationOppoInfo.setName(modelRelationInfo.getRelationName());
                        }
                    } catch (Exception e) {
                        log.warn("extractFrom {}", e.toString());
                    }
                }
                modelList.add(modelRelationOppoInfo);
            }
        }
    }
}
